import Notify from '@vant/weapp/notify/notify';
const app = getApp()

Page({
  data: {
    taskName: '',
    taskList: []
  },
  onShow() {
    this.setData({
      taskList: app.globalData.taskList
    })
  },
  addTask() {
    if (!this.data.taskName) {
      Notify({
        type: 'danger',
        message: '任务名称不得为空'
      });
      return
    }
    const taskItem = {
      id: new Date().getTime(),
      taskName: this.data.taskName,
      selected: false
    }
    this.setData({
      taskList: [...this.data.taskList, taskItem],
      taskName: ''
    })
    app.globalData.taskList.push(taskItem)
  },
  setTaskList(e) {
    this.setData({
      taskList: e.detail
    })
    app.globalData.taskList = e.detail
  },
  deleteTaskItem(e) {
    const newTaskList = this.data.taskList.filter(item => item.id !== e.detail)
    this.setData({
      taskList: newTaskList
    })
    app.globalData.taskList = newTaskList
  }
})